﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using System.Drawing;
using System.Diagnostics;
using Silvermoon.Controls.Interfaces;
using Silvermoon.Core;

namespace Silvermoon.Controls.Base
{
    public class Decorator : Control, IDecorator
    {
        private Control[] controls;
        private Control control;

        public Control Control { get { return control; } set { if (control != value) OnControlChange(value); } }

        protected virtual void OnControlChange(Control value)
        {
            this.control = value;
            if (value != null)
            {
                if (controls == null) controls = new Control[1];
                this.controls[0] = value;
            }
            else
            {
                controls = null;
            }
            Invalidate(InvalidationFlag.LayoutAppearance);
        }

        protected override void OnLayout()
        {
            base.OnLayout();
            bool s = (InvalidationMask & InvalidationFlag.Size) != 0;
            if (CheckFlag(InvalidationFlag.Size))
            {
                Control c = Control;
                if (c != null) LayoutEngine.Layout(c, Size); //c.Bounds = new Rectangle(0, 0, Width, Height);
            }
        }

        protected internal override IList<Control> SubControls { get { return controls; } }
    }
}
